package com.cgy.utils.base.tuple;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 元组
 * @author CHTK
 */
public abstract class Tuple implements Iterable<Object>, Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 2218237364060903656L;
	/**
     * 数据集
     */
    private final Object[] valueArray;
    /**
     * 数据集
     */
    private final List<Object> valueList;

    protected Tuple(final Object... values) {
        super();
        this.valueArray = values;
        this.valueList = Arrays.asList(values);
    }

    /**
     * 获取长度
     * @return
     */
    public abstract int getSize();

    /**
     * 获取值
     * @param pos
     * @return
     */
    public final Object getValue(final int pos) {
        if (pos >= getSize()) {
            throw new IllegalArgumentException(
                    "Cannot retrieve position " + pos + " in " + this.getClass().getSimpleName() +
                            ". Positions for this class start with 0 and end with " + (getSize() - 1));
        }
        return this.valueArray[pos];
    }

    @Override
    public final Iterator<Object> iterator() {
        return this.valueList.iterator();
    }

    @Override
    public final String toString() {
        return this.valueList.toString();
    }

}
